package cn.sanch.order.service.impl;

import cn.sanch.basic.loginContext.LoginContext;
import cn.sanch.basic.util.PageList;
import cn.sanch.order.domain.OrderPetAcquisition;
import cn.sanch.order.dto.OrderDto;
import cn.sanch.order.mapper.OrderPetAcquisitionMapper;
import cn.sanch.order.query.OrderPetAcquisitionQuery;
import cn.sanch.order.service.IOrderPetAcquisitionService;
import cn.sanch.basic.service.impl.BaseServiceImpl;
import cn.sanch.org.domain.Employee;
import cn.sanch.org.domain.Shop;
import cn.sanch.org.mapper.EmployeeMapper;
import cn.sanch.org.mapper.ShopMapper;
import cn.sanch.pet.domain.Pet;
import cn.sanch.pet.domain.SearchMasterMsg;
import cn.sanch.pet.mapper.PetMapper;
import cn.sanch.pet.mapper.SearchMasterMsgMapper;
import cn.sanch.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;

/**
 * 业务实现类：
 */
@Service
public class OrderPetAcquisitionServiceImpl extends BaseServiceImpl<OrderPetAcquisition> implements IOrderPetAcquisitionService {


    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;

    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;

    @Autowired
    private PetMapper petMapper;

    @Override
    public PageList<OrderPetAcquisition> queryPage(OrderPetAcquisitionQuery query, HttpServletRequest request) {
        // 根据请求调用工具类获取登录人信息
        Logininfo logininfo = LoginContext.getLogininfo(request);
        // 根据登录人id获取员工对象
        Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());

        // 判断该员工有没有shopId
        if (employee.getShop_id() != null) {
            // shopId不为null就表示要么是店铺管理员要么就是店铺员工

            // 根据employee对象查询店铺
            Shop shop = shopMapper.findOne(employee.getShop_id());

            // 根据店铺对象的管理员id,再和employee的id比较
            if (employee.getId() != shop.getAdmin_id()) {
                // 如果不相等，就表示是店员
                query.setEmpId(employee.getId());
            }
            query.setShopId(shop.getId());
        }
        return super.queryPage(query);
    }

    /**
     * 订单确认
     * @param orderDto
     */
    @Override
    public void confirm(OrderDto orderDto) {

        // 1.获取参数
        // 订单id
        Long orderId = orderDto.getId();
        // 付款金额
        BigDecimal money = orderDto.getMoney();
        // 支付方式
        Integer paytype = orderDto.getPaytype();

        // 2.修改订单 - state=1、金额、支付方式
        // 查询订单对象
        OrderPetAcquisition order = orderPetAcquisitionMapper.findOne(orderId);
        order.setState(1);
        order.setPrice(money);
        order.setPaytype(paytype);
        orderPetAcquisitionMapper.update(order);

        // 3.修改寻主消息对象的状态
        SearchMasterMsg msg = searchMasterMsgMapper.findOne(order.getSearchMasterMsgId());
        msg.setState(2);
        msg.setNote("处理成功，交易完成！");
        searchMasterMsgMapper.update(msg);

        // 4.修改宠物对象的成本价
        Pet pet = petMapper.findOne(order.getPetId());

        pet.setCostprice(money);
        petMapper.update(pet);
    }


    /**
     * 订单取消
     * @param id
     */
    @Override
    public void delete(Long id) {
        // 1.置空寻主对象的shopId
        // 获取订单对象
        OrderPetAcquisition order = orderPetAcquisitionMapper.findOne(id);
        // 通过寻主对象id置空shopId
        searchMasterMsgMapper.reject(order.getSearchMasterMsgId());
        // 删除宠物对象 - 没有收购成功才会取消订单
        petMapper.delete(order.getPetId());

        super.delete(id);
    }
}
